﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for Utilities
/// </summary>
public class Utilities
{
	public Utilities()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    private const String PAGE_SIZE = "PAGE_SIZE";
    private const String NEWS_NUMBER = "NEWS_NUMBER";

    //private static string currentUserName;
    /// <summary>
    /// Get current User Name
    /// </summary>
    public static int CurrentUserID
    {
        get
        {
            try
            {
                int currentUserName = Convert.ToInt16(HttpContext.Current.User.Identity.Name);
                return currentUserName;
            }
            catch (Exception exception)
            {
               
                return -1;
            }
        }
    }


    /// <summary>
    /// Get curent UserName from Session
    /// If cannot get username from session, query from database then store into session
    /// </summary>
    public static String CurrentUserName
    {
        get
        {
            String username = HttpContext.Current.Session[WebsiteConstant.USER_NAME] as String;

            if (String.IsNullOrEmpty(username))
            {
                using (DatabaseDataContext context = DataFactory.GetDataContext())
                {
                    username = (from u in context.Users
                                where u.UserID == CurrentUserID
                                select u.UserName).SingleOrDefault();
                    HttpContext.Current.Session[WebsiteConstant.USER_NAME] = username;
                    return username;
                }
            }

            return username;

        }

    }

    public static String CurrentFullName
    {
        get
        {
            String username = HttpContext.Current.Session[WebsiteConstant.FULL_NAME] as String;

            if (String.IsNullOrEmpty(username))
            {
                using (DatabaseDataContext context = DataFactory.GetDataContext())
                {
                    username = (from u in context.Users
                                where u.UserID == CurrentUserID
                                select u.FullName).SingleOrDefault();
                    HttpContext.Current.Session[WebsiteConstant.FULL_NAME] = username;
                    return username;
                }
            }

            return username;

        }

    }

    public static void ShowMessage(string MessageText, Page MyPage)
    {
        MyPage.ClientScript.RegisterStartupScript(MyPage.GetType(),
            "MessageBox", "alert('" + MessageText.Replace("'", "\'") + "');", true);

    }


    public static string GenerateRandomCode()
    {
        Random random = new Random();
        string s = String.Empty;
        for (int i = 0; i < 6; i++)
            s = String.Concat(s, random.Next(10).ToString());
        return s;
    }

    #region int GetPageSize()
    /// <summary>
    /// Get page size from Web.config file
    /// </summary>
    /// <returns>Page size</returns>
    public static int GetPageSize()
    {
        try
        {
            return Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings[PAGE_SIZE]);
        }
        catch (Exception)
        {
            return 10;
        }
    }
    #endregion



    #region int GetNewsNumber()
    /// <summary>
    /// Get news number
    /// </summary>
    /// <returns></returns>
    public static int GetNewsNumber()
    {
        return Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings[NEWS_NUMBER]);
    }
    #endregion

    #region String GetHeadLine(String content, int length)
    /// <summary>
    /// Get head line
    /// </summary>
    /// <param name="content"></param>
    /// <param name="length"></param>
    /// <returns></returns>
    public static String GetHeadLine(String content, int length)
    {
        String des = Regex.Replace(content, "<.*?>", String.Empty);
        if (length < des.Length)
            des = des.Substring(0, length);

        return des;
    }
    #endregion  
}